package com.gosophia.conference.action.summary;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.commons.web.logAnnotation.LogRecord;
import com.gosophia.conferenceManagement.ConferenceTurningAction;
import com.gosophia.conferenceManagement.common.dto.DocumentDTO;
import com.gosophia.conferenceManagement.summary.dto.SummaryJobDataDTO;
import com.gosophia.conferenceManagement.summary.ws.SummaryWebService;
import com.gosophia.oa.action.JudgeCurrentUndertaker;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.opensymphony.xwork2.ActionContext;

/**
 * 同意并提交下一人
 * @author mayue
 *
 */
@Scope("prototype")
@Controller("AgreeSubmitNextPersonAction")
public class AgreeSubmitNextPersonAction extends BaseSupportAction {

    private static final long serialVersionUID = 1L;
    @Autowired
    private SummaryWebService summaryWebService;
    @Autowired
    private JudgeCurrentUndertaker judgeCurrentUndertaker;
    
    private SummaryJobDataDTO summaryJobDataDTO;
    
    private List<Long> oldDocumentIds;
    
    //在线文档在仓库中的路径
    private String onlinePath;
    //在线文档版本
    private String onlineVersion;
    @Override
    @LogRecord(eventAction="会议管理-会议总结-提交下一人审批",eventTarget="summaryJobDataDTO")
    public String _execute() throws Exception {
        
        //判断当前用户和job承办人是否匹配,如果不匹配转到异常页面
        judgeCurrentUndertaker.judgeOperationAuthority(summaryJobDataDTO.getJobDTO().getJobId());
        
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
        .getContext().getAuthentication().getPrincipal();
        
        Long operatorId = userDetails.getUserAccountDTO().getUserInfoDTO()
        .getUserInfoId();
        
        List<DocumentDTO> oldDocumentList = new ArrayList<DocumentDTO>();
        //附件
        oldDocumentList = summaryWebService.dealOldAttachment(oldDocumentIds);
        //在线文档
        DocumentDTO onlineDocumentDTO = new DocumentDTO();
        onlineDocumentDTO.setDocumentPath(onlinePath);
        onlineDocumentDTO.setDocumentVersion(onlineVersion);
        onlineDocumentDTO.setDocumentCategory("online");
        
        summaryJobDataDTO.setAttachmentsDTOs(oldDocumentList);
        summaryJobDataDTO.setOnlineDocumentDTO(onlineDocumentDTO);
        
        //同意并提交下一人
        summaryJobDataDTO.setTurningAction(ConferenceTurningAction.SUBMIT_NEXT_APPROVAL.getValue());//ConferenceTurningAction.SUBMIT_NEXT_APPROVAL.getValue()
        summaryWebService.HandleConferenceTurningSummary(summaryJobDataDTO.getJobDTO().getJobId(), operatorId, summaryJobDataDTO);
        
        super.getRequestResult().setRsltCode("0");
        super.getRequestResult().setMessage("会议总结审核并提交下一人成功");
        ActionContext.getContext().getSession().put("_lastOpResult", super.getRequestResult());
        
        if("task".equals(this.getFromFlag())){
            return "task";
        }
        return SUCCESS;
    }


    public SummaryJobDataDTO getSummaryJobDataDTO() {
        return summaryJobDataDTO;
    }


    public void setSummaryJobDataDTO(SummaryJobDataDTO summaryJobDataDTO) {
        this.summaryJobDataDTO = summaryJobDataDTO;
    }


    public List<Long> getOldDocumentIds() {
        return oldDocumentIds;
    }


    public void setOldDocumentIds(List<Long> oldDocumentIds) {
        this.oldDocumentIds = oldDocumentIds;
    }


    public String getOnlinePath() {
        return onlinePath;
    }


    public void setOnlinePath(String onlinePath) {
        this.onlinePath = onlinePath;
    }


    public String getOnlineVersion() {
        return onlineVersion;
    }


    public void setOnlineVersion(String onlineVersion) {
        this.onlineVersion = onlineVersion;
    }
    

}
